package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: QingYingYan
 * @Date: 2025-10-20 21:13
 * @Description:
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    DishService dishService;

    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dto) {
        log.info("新增菜品");
        dishService.save(dto);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查菜品");
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result getId(@PathVariable Long id) {
        DishVO dishVO = dishService.getId(id);

        return Result.success(dishVO);
    }

    @ApiOperation("修改菜单")
    @PutMapping
    public Result upData(@RequestBody DishDTO dishDTO) {
        log.info("修改菜单");
        dishService.upData(dishDTO);
        return Result.success();
    }

    @ApiOperation("修改状态")
    @PostMapping("/status/{status}")
    public Result upStatus(@PathVariable Integer status ,Long id){
        dishService.upStatus(status,id);
        return Result.success();
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId, String name){
        log.info("根据分类id查询菜品: categoryId={}, name={}",categoryId, name);
        List<Dish> dishes = dishService.getByCategoryId(categoryId, name);
        return Result.success(dishes);
    }
}
